/* 
 * File:   DocumentParser.cpp
 * Author: taylor
 * 
 * Created on November 7, 2012, 2:25 PM
 */

#include "DocumentParser.h"

#include "Stemmer.c"

void DocumentParser::parseInfo(vector<string>& aStr) {
    for(int i = 0; i < aStr.size(); i++) {
        removePunctuation(aStr.at(i));
    }
    removeStopWords(aStr);
    stemVector(aStr);
    removeCopies(aStr);
}

void DocumentParser::breakIntoTokens(string str, vector<string>& aStr) {
    stringstream ss;
    string temp;
    
    ss << str;
    
    while(ss >> temp)
        aStr.push_back(temp);
}

//STRIPPING
string DocumentParser::removePunctuation(string& str) {
     for(int i=0; i <str.length(); i++) {
             if(ispunct(str[i]))
                  str[i]= ' ';
     }
     str.erase(remove(str.begin(),str.end(),' '),str.end());
     transform(str.begin(), str.end(), str.begin(), ::tolower);
     return str;
}

void DocumentParser::removeStopWords(vector<string>& aStr) {
    stopWordFile = "StopWordsList.txt";
    fstream fin;
    
    fin.open(stopWordFile.c_str(), fstream::in);
    if(fin) {
        string str;
        while(fin >> str) {
                for(int i = 0; i < aStr.size(); i++) {
                    if(str.compare(aStr.at(i)) == 0) {
                        aStr.erase(aStr.begin() + i);
                    }
                }
        }
    } else
        cout << "Stop word file could not be opened.";
    fin.close();
}

void DocumentParser::removeCopies(vector<string>& vec) {
    sort( vec.begin(), vec.end() );
    vec.erase( unique( vec.begin(), vec.end() ), vec.end() );
}

void DocumentParser::stemVector(vector<string> &aStr) {
    for(int i = 0; i < aStr.size(); i++) {
        string str;
        string temp;
        
        str = aStr.at(i);
        char* s = &str[0];
        
        int x = stem(s, 0, str.length()-1);
        
        temp = str.substr(0, x);
        
        aStr.at(i) = temp;
    }
}